package com.icbc.open.chengdu.demo.settlement.holdunhold;

import com.csii.pp.icbc.util.MD5Util;
import com.csii.pp.icbc.util.Util;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import com.icbc.api.request.SettlementAccountHoldRequestV1;
import com.icbc.api.response.SettlementAccountHoldResponseV1;
import com.icbc.open.chengdu.demo.BaseData;
import com.icbc.open.chengdu.demo.utils.GenerateKeyUtils;
import com.icbc.open.chengdu.demo.utils.SecretUtils;

/**
 * 结算账户保留
 *
 * @author xcz
 */
public class SettlementAccountHoldTest {

    public static void main(String[] args) throws Exception {
        String sm4Key = GenerateKeyUtils.generateSM4Key();//生成SM4密钥
        DefaultIcbcClient client = new DefaultIcbcClient(BaseData.APP_ID, BaseData.MY_RSA_PRIVATE_KEY, BaseData.ICBC_RSA_PUBLIC_KEY);
        SettlementAccountHoldRequestV1 request = new SettlementAccountHoldRequestV1();
        SettlementAccountHoldRequestV1.SettlementAccountHoldRequestV1Biz bizContent = new SettlementAccountHoldRequestV1.SettlementAccountHoldRequestV1Biz();
        request.setServiceUrl(BaseData.ACCOUNT_HOLD_URL);//API地址
        bizContent.setCorpNo(BaseData.APP_ID); //合作方机构编号
        bizContent.setTrxAccDate(BaseData.TEST_TRX_ACC_DATE); //合作方交易日期
        bizContent.setTrxAccTime(BaseData.TEST_TRX_ACC_TIME); //合作方交易时间
        bizContent.setCorpDate(BaseData.TEST_CORP_DATE); //合作方工作日期
        bizContent.setCorpSerno(Util.getNonceStr(16)); //合作方交易单号
        bizContent.setOutServiceCode("accounthold"); //外部服务代码
        bizContent.setMediumId(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_ACCOUNT_NUMBER_2)); //工行联名卡号
        bizContent.setCcy(1); //币种
        bizContent.setCashExFlag(9); //钞汇标志
        bizContent.setAmount(BaseData.TEST_HOLD_AMOUNT); //交易金额
        bizContent.setHoldType(BaseData.TEST_HOLD_TYPE);  //保留种类
        bizContent.setSummary("DY"); //摘要
        bizContent.setRemarks(BaseData.TEST_HOLD_REMARKS); //注释
        bizContent.setSecretKey(SecretUtils.encryptSM2(sm4Key)); //sm4对称密钥
        bizContent.setMediumIdHash(MD5Util.string2MD5(BaseData.TEST_ACCOUNT_NUMBER_2)); //卡号哈希值
        request.setBizContent(bizContent);
        SettlementAccountHoldResponseV1 response = client.execute(request);
        System.out.println(JSON.toJSONString(response));
        if (response.isSuccess() && response.getReturnCode() == 0) {
            // 业务成功处理
            System.out.println("实际余额："+response.getAccountBalance());
            System.out.println("可用余额："+response.getAccountUseamt());
        } else {
            // 失败
        }

    }
}
